package com.paomi.client.fragment.information;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.newsDetail.model.NewsListJson;
import com.paomi.client.base.BaseMvpFragment;
import com.paomi.client.fragment.information.Adapter.InformaticaAdapter;
import com.paomi.client.fragment.information.presenter.InformaticaFraPre;
import com.paomi.client.net.Constants;
import com.paomi.client.refresh.AdapterWrapper;
import com.paomi.client.refresh.SwipeToLoadHelper;
import com.paomi.client.widget.header.HeaderGetView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by pc on 2018/10/25
 * Describe:分类中资讯列表
 * com.paomi.client.fragment
 */
public class InformaticaFragment extends BaseMvpFragment<InformationMvpView, InformaticaFraPre> implements InformationMvpView,SwipeToLoadHelper.LoadMoreListener {
    InformaticaAdapter adapter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_notfound)
    ImageView ivNotfound;
    @BindView(R.id.tv_notfound)
    TextView tvNotfound;
    @BindView(R.id.ll_none)
    LinearLayout llNone;
    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    private String getData = "";
    private String classifyid = "0";
    private boolean get;//是否刷新
    private int pageNum = 1;
    private String check;

    List<NewsListJson> list = new ArrayList<>();

    //上拉加载更多
    private AdapterWrapper mAdapterWrapper;
    private SwipeToLoadHelper mLoadMoreHelper;

    public static InformaticaFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        InformaticaFragment fragment = new InformaticaFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.layout_only_rec;
    }

    @Override
    protected void initEvent() {
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        adapter = new InformaticaAdapter(this.getActivity());
        recyclerView.setAdapter(adapter);

        mAdapterWrapper = new AdapterWrapper(adapter);
        mLoadMoreHelper = new SwipeToLoadHelper(recyclerView, mAdapterWrapper);
        mLoadMoreHelper.setLoadMoreListener(this);
        recyclerView.setAdapter(mAdapterWrapper);

        ivNotfound.setImageResource(R.mipmap.img_category_information_empty);
        tvNotfound.setText(getResources().getString(R.string.empty_news));
        if (list.size() > 0) {
            llNone.setVisibility(View.GONE);
        } else {
            llNone.setVisibility(View.VISIBLE);
        }
        //网络请求
        if (getPresenter() != null) {
            getPresenter().setInfoData(pageNum, getData, classifyid);
        } else {
            presenter = new InformaticaFraPre();
            presenter.setInfoData(pageNum, getData, classifyid);
        }
        HeaderGetView header=new HeaderGetView(getActivity());
        ptrRefresh.setHeaderView(header);
        ptrRefresh.addPtrUIHandler(header);
        //下拉刷新
        ptrRefresh.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                pageNum=1;
                if (getPresenter() != null) {
                    getPresenter().setInfoData(pageNum, getData, classifyid);
                } else {
                    presenter = new InformaticaFraPre();
                    presenter.setInfoData(pageNum, getData, classifyid);
                }
                ptrRefresh.refreshComplete();
            }
        });
    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        if (!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);
//        }
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
//    }

    @Override
    public InformaticaFraPre createPresenter() {
        return new InformaticaFraPre();
    }

    @Override
    public void getInfoData(List<NewsListJson> result) {
        if (pageNum == 1) {
            list.clear();
        }
        if (result != null) {
            if (result.size() > 0) {
                llNone.setVisibility(View.GONE);
                list.addAll(result);
                adapter.setData(list);
            } else {
                llNone.setVisibility(View.VISIBLE);
            }
        }
        mLoadMoreHelper.setLoadMoreFinish();
        mAdapterWrapper.notifyDataSetChanged();
    }

    //    @Subscribe
//    public void onEvent(NewsJson.NewsBean result) {
//
//    }
    public void setListData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            getData = bundle.getString("getData");
            classifyid = bundle.getString("classify_id");
            get = bundle.getBoolean("classify_get");
            check = bundle.getString("checkBox");
            Log.e("check", check);
            if (get) {
                //网络请求
                if (getPresenter() != null) {
                    getPresenter().setInfoData(pageNum, getData, classifyid);
                } else {
                    presenter = new InformaticaFraPre();
                    presenter.setInfoData(pageNum, getData, classifyid);
                }

            }

        }
    }

    @Override
    public void onLoad() {
        if(list.size()%Constants.PAGE_SIZE ==0){
            pageNum++;
            if (getPresenter() != null) {
                getPresenter().setInfoData(pageNum, getData, classifyid);
            } else {
                presenter = new InformaticaFraPre();
                presenter.setInfoData(pageNum, getData, classifyid);
            }
        }else{//针对GridLayoutManager的访问
          //移除FooterView
            mAdapterWrapper.setLoadItemState(false);
        }
    }
}
